package com.nus.services;

import com.nus.beans.*;
import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/16/12
 * Time: 1:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConversionService {
    private Logger logger = Logger.getLogger(this.getClass());

    public UserType getUserType(int dbUserType) {
        UserType userType;

        switch (dbUserType) {
            case 1:
                userType = UserType.DEVELOPER;
                break;
            case 2:
                userType = UserType.PM;
                break;
            case 3:
                userType = UserType.HR_MGR;
                break;
            case 4:
                userType = UserType.HR_ASST;
                break;
            default:
                logger.error("User type is not supported by the system");
                throw new IllegalArgumentException("User type is not supported by the system");
        }
        return userType;
    }

    public int getUserType(UserType userType) {
        int userTypeDB;

        switch (userType) {
            case DEVELOPER:
                userTypeDB = 1;
                break;
            case PM:
                userTypeDB = 2;
                break;
            case HR_MGR:
                userTypeDB = 3;
                break;
            case HR_ASST:
                userTypeDB = 4;
                break;
            default:
                logger.error("User type is not supported by the system");
                throw new IllegalArgumentException("User type is not supported by the system");
        }
        return userTypeDB;
    }

    public int getLeaveStatus(LeaveStatus status) {
        int dbStatus = -1;
        switch (status) {
            case APPLIED:
                dbStatus = 1;
                break;
            case VERIFIED:
                dbStatus = 2;
                break;
            case APPROVED:
                dbStatus = 3;
                break;
            case CANCELED:
                dbStatus = 4;
                break;
            case REJECTED:
                dbStatus = 5;
                break;
            default:
                logger.error("Leave status is not mapped to database. Status = " + status);
                throw new IllegalArgumentException("Leave status is not supported by database");
        }
        return dbStatus;

    }

    public LeaveStatus getLeaveStatus(int leaveStatusDB) {
        LeaveStatus leaveStatus;
        switch (leaveStatusDB) {
            case 1:
                leaveStatus = LeaveStatus.APPLIED;
                break;
            case 2:
                leaveStatus = LeaveStatus.VERIFIED;
                break;
            case 3:
                leaveStatus = LeaveStatus.APPROVED;
                break;
            case 4:
                leaveStatus = LeaveStatus.CANCELED;
                break;
            case 5:
                leaveStatus = LeaveStatus.REJECTED;
                break;
            default:
                logger.error("Leave status is not mapped. Status = " + leaveStatusDB);
                throw new IllegalArgumentException("Leave status is not supported by application.");
        }
        return leaveStatus;
    }

    public UserStatus getUserStatus(int userStatusDB) {
        UserStatus userStatus;
        switch (userStatusDB) {
            case 1:
                userStatus = UserStatus.ACTIVE;
                break;
            case 2:
                userStatus = UserStatus.DELETED;
                break;
            case 3:
                userStatus = UserStatus.BLOCKED;
                break;
            default:
                logger.error("User status is not mapped. Status = " + userStatusDB);
                throw new IllegalArgumentException("Leave status is not supported by application.");
        }
        return userStatus;
    }

    public UserSubType getUserSubType(int userSubTypeDB) {
        UserSubType userSubType;
        switch (userSubTypeDB) {
            case 1:
                userSubType = UserSubType.ASE;
                break;
            case 2:
                userSubType = UserSubType.SE;
                break;
            case 3:
                userSubType = UserSubType.SSE;
                break;
            case 4:
                userSubType = UserSubType.NONE;
                break;
            default:
                logger.error("User sub type is not mapped. Status = " + userSubTypeDB);
                throw new IllegalArgumentException("Leave status is not supported by application.");
        }
        return userSubType;
    }

    public int getUserSubTypeDB(UserSubType userSubType) {
        int userSubTypeDB = -1;
        switch (userSubType) {
            case ASE:
                userSubTypeDB = 1;
                break;
            case SE:
                userSubTypeDB = 2;
                break;
            case SSE:
                userSubTypeDB = 3;
                break;
            case NONE:
                userSubTypeDB = 4;
                break;
            default:
                logger.error("User sub type is not mapped. Status = " + userSubType);
                throw new IllegalArgumentException("Leave status is not supported by application.");
        }
        return userSubTypeDB;
    }

    public int getUserStatusDB(UserStatus userStatus) {
        int userStatusDB;
        switch (userStatus) {
            case ACTIVE:
                userStatusDB = 1;
                break;
            case DELETED:
                userStatusDB = 2;
                break;
            default:
                logger.error("User status is not mapped. Status = " + userStatus);
                throw new IllegalArgumentException("Leave status is not supported by database.");
        }
        return userStatusDB;
    }

    public int getProjectStatusDB(ProjectStatus projectStatus) {
        int projectStatusDB;
        switch (projectStatus) {
            case ACTIVE:
                projectStatusDB = 1;
                break;
            case COMPLETED:
                projectStatusDB = 2;
                break;
            case DELETED:
                projectStatusDB = 3;
                break;
            default:
                logger.error("Project status is not mapped. Status = " + projectStatus);
                throw new IllegalArgumentException("Project status is not supported by database.");
        }
        return projectStatusDB;
    }

    public ProjectStatus getProjectStatus(int projectStatusDB) {
        ProjectStatus projectStatus;
        switch (projectStatusDB) {
            case 1:
                projectStatus = ProjectStatus.ACTIVE;
                break;
            case 2:
                projectStatus = ProjectStatus.COMPLETED;
                break;
            case 3:
                projectStatus = ProjectStatus.DELETED;
                break;
            default:
                logger.error("Project status is not mapped. Status = " + projectStatusDB);
                throw new IllegalArgumentException("Project status is not supported by database.");
        }
        return projectStatus;
    }

    public LeaveStatus getLeaveStatusByUserType(UserType userType) {
        LeaveStatus leaveStatus = LeaveStatus.APPLIED;
        switch (userType) {
            case DEVELOPER:
                leaveStatus = LeaveStatus.APPLIED;
                break;
            case PM:
                leaveStatus = LeaveStatus.VERIFIED;
                break;
            case HR_MGR:
                leaveStatus = LeaveStatus.APPROVED;
                break;
            case HR_ASST:
                leaveStatus = LeaveStatus.VERIFIED;
                break;
        }
        return leaveStatus;
    }

    public Date getDate(int year, int month, int date) {
        Date generatedDate = null;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setLenient(false);
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.DATE, date);
            generatedDate = new Date(calendar.getTimeInMillis());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return generatedDate;
    }

     public Date getDate(String year, String month, String date) {
        return getDate(Integer.parseInt(year),Integer.parseInt(month), Integer.parseInt(date));
     }


}
